//
//  BBDay.swift
//  BBLinkiOSProject
//
//  Created by liuzhu on 2018/12/4.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit

class BBDay: NSObject {
   @objc var userId:String?
   @objc var dayId:String?
   @objc var times:NSNumber?
   @objc var totalTime:NSNumber?
   @objc var totalDistance:NSNumber?
   @objc var clockDes:String?
   @objc var isclocked:NSNumber?
   @objc var clockImage:String?
   @objc var clockLines:String?
   @objc var createTime:NSNumber?
   @objc var updateTime:NSNumber?
    
    //字典转模型
    init(dict:[String:AnyObject]){
        super.init()
        setValuesForKeys(dict)
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        print(key)
    }
    
    class func loadData(dayId:String,finish:@escaping (_ model:BBDay?,_ error:Error?)->()){
        let path = "day/select_day.do"
        let params = ["token":BBUser.loadUser()?.token, "userId":BBUser.loadUser()?.userId,"dayId":dayId]
        AFNetWorkManager.shareNetworkTools().post(path, parameters: params, success: { (_, JSON) in
            let dict = JSON as![String:AnyObject]
            let dictStatus = dict["status"]as!NSNumber
            if(dictStatus == 0){
                let data = dict["data"]
                let model = dict2Model(dict: data as! [String:AnyObject])
                finish(model,nil)
            }else if(dictStatus == 10){
                BBUser.removeUser()
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: BBSwitchRootViewControllerKey), object: false)
            }
            else{
                finish(nil,nil)
            }
        }) { (_, error) in
            finish(nil,error)
        }
    }
    
    //将字段数组转换为模型数组
    class func dict2Model(dict:[String:AnyObject])->BBDay{
        return BBDay(dict: dict)
    }
    
    
}
